export function change(val) {
  if (val) {
    return JSON.parse(val)
  } else {
    return []
  }
}




//轻提示toast
export function setToast(
  {title, icon = "none", duration = 1500, mask = false} = {}) {
  wx.showToast({
    title,
    icon,
    duration,
    mask
  });
}

//模态提示框
export function setModal(
  {
    title = "",
    content = "",
    showCancel = true,
    cancelText = "取消",
    cancelColor = "#363636",
    confirmText = "确定",
    confirmColor = "#ffbd4a",
    confirm = () => {
      return;
    },
    cancel = () => {
      return;
    },
    fail
  } = {}) {
  wx.showModal({
    title: title,
    content: content,
    showCancel: showCancel,
    cancelText: cancelText,
    cancelColor: cancelColor,
    confirmText: confirmText,
    confirmColor: confirmColor,
    success(res) {
      if (res.confirm) {
        confirm();
      } else if (res.cancel) {
        cancel();
      }
    },
    fail() {
      fail();
    }
  });
}

function formatNumber(n) {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}

export function formatTime(date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  const t1 = [year, month, day].map(formatNumber).join('/')
  const t2 = [hour, minute, second].map(formatNumber).join(':')

  return `${t1} ${t2}`
}

export default {
  formatNumber,
  formatTime
}
